package com.qyhl.cloud.webtv.module_integral.wallet;

import android.content.Intent;
import android.text.TextUtils;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.cloud.webtv.module_integral.R;
import com.qyhl.cloud.webtv.module_integral.R2;
import com.qyhl.cloud.webtv.module_integral.wallet.goldcoin.GlodFragment;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.ContextUtilts;
import com.qyhl.webtv.basiclib.utils.SpanUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.intergral.WallerBean;
import com.qyhl.webtv.commonlib.utils.config.HomeThemeColorUtil;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

/**
 * 钱包
 */
@Route(path = ARouterPathConstant.WALLER, extras = ARouterPathConstant.LOGIN_NEEDED)
public class WalletActivity extends BaseActivity implements WallerContract.WallerView {
    @BindView(R2.id.wallertab)
    SlidingTabLayout wallertab;
    @BindView(R2.id.waller_vp)
    ViewPager waller_vp;
    @BindView(R2.id.yesterdayGold)
    TextView yesterdayGold;
    @BindView(R2.id.introduce)
    TextView introduce;
    @BindView(R2.id.todayGold)
    TextView todayGold;
    @BindView(R2.id.myGold)
    TextView myGold;
    @BindView(R2.id.myMoney)
    TextView myMoney;

    private WallerContract.WallerPresenter presenter;
    private GlodFragment glodFragment;


    private void initView() {
        presenter = new WalletPresenter(this);
        WalletPresenter.MAdapter tabAdapter = presenter.getTabAdapter(this);
        //获取金币fargment
        glodFragment = (GlodFragment) tabAdapter.getItem(0);
        waller_vp.setAdapter(tabAdapter);
        wallertab.setViewPager(waller_vp);
        wallertab.setTextSelectColor(ContextCompat.getColor(ContextUtilts.getInstance().getmContext(), R.color.global_base_0));
        wallertab.setTextUnselectColor(HomeThemeColorUtil.getTabNormalColor());
        wallertab.getTitleView(0).getPaint().setFakeBoldText(true);

        setText(yesterdayGold, 0, "昨日金币", null);
        setText(todayGold, 0, "今日金币", null);
        setText(myGold, 0, "我的金币", null);
        setText(myMoney, 0, "我的零钱", " (元)");

        getData();
        presenter.getCoinRate();
    }

    //封装方法让 Fragment 能够调用
    public void getData() {
        presenter.getData();
    }

    //设置文本
    private void setText(TextView yesterdayGold, float i, String text, String msg) {
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        SpanUtils spanUtils = new SpanUtils(this);
        spanUtils.append(decimalFormat.format(i)).setSpans(new TextAppearanceSpan(this,
                R.style.inter_task_center_special_two2));
        spanUtils.append("\n" + text);
        if (!TextUtils.isEmpty(msg)) {
            spanUtils.append(msg).setSpans(new TextAppearanceSpan(this,
                    R.style.inter_goldcointext));
        }
        yesterdayGold.setText(spanUtils.create());
    }

    @OnClick({R2.id.back, R2.id.exchangegold})
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.back) {
            finish();

        } else if (i == R.id.exchangegold) {
            RouterManager.router(ARouterPathConstant.EXCHANGE_GOLD, this, 999);
        } else {
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 999 && resultCode == 666) {
            presenter.getData();
        }
    }

    @Override
    public void getDataError(String message) {
        Toasty.normal(this, message).show();
        //通知 Fragment 数据获取失败
        glodFragment.dataError(message);
    }

    @Override
    public void getDataSuccess(WallerBean data) {
        setText(yesterdayGold, data.getYesterdayCoin(), "昨日金币", null);
        setText(todayGold, data.getTodayCoin(), "今日金币", null);
        setText(myGold, data.getCurrCoin(), "我的金币", null);
        setText(myMoney, data.getCurrMoney(), "我的零钱", " (元)");
        //通知 Fragment 展示数据
        glodFragment.setBean(data);
    }

    @Override
    public void getCoinRateSuccess(int rate) {
        introduce.setText("金币兑零钱比值，会随整体收益变动，当前为" + rate + "金币=1元");
    }

    @Override
    protected void onDestroy() {
        presenter.onDestroy();
        super.onDestroy();
    }

    @Override
    protected int getContentView() {
        return R.layout.inter_activity_wallet;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        initView();
    }

    @Override
    protected void setListener() {

    }
}
